home *** CD-ROM | disk | FTP | other *** search
/ Mac Mania 5 / MacMania 5.toast / / Internet software / NewsWatcher / NW Source / Source / log.c < prev    next >
Text File  |  1997-01-09  |  4KB  |  168 lines

  1. /*----------------------------------------------------------------------------
  2.  
  3.     log.c
  4.  
  5.     This module handles logging.
  6.     
  7.     Copyright © 1994-1997, Northwestern University.
  8.  
  9. ----------------------------------------------------------------------------*/
  10.  
  11. #include <stdio.h>
  12. #include <string.h>
  13.  
  14. #include "glob.h"
  15. #include "log.h"
  16. #include "dialog.h"
  17. #include "strutil.h"
  18. #include "fileutil.h"
  19. #include "resutil.h"
  20. #include "ic.h"
  21.  
  22.  
  23.  
  24. static short gRefNum = 0;
  25.  
  26.  
  27.  
  28. /*----------------------------------------------------------------------------
  29.     PutString 
  30.     
  31.     Write a string to the log file.
  32.     
  33.     Entry:    str = string to write.
  34. ----------------------------------------------------------------------------*/
  35.  
  36. static void PutString (char *str)
  37. {
  38.     unsigned long secs;
  39.     DateTimeRec d;
  40.     char line[1024];
  41.     long len;
  42.     
  43.     GetDateTime(&secs);
  44.     Secs2Date(secs, &d);
  45.     sprintf(line, "%.2d/%.2d/%.2d %.2d:%.2d:%.2d %s\r",
  46.         d.month, d.day, d.year, d.hour, d.minute, d.second, str);
  47.     len = strlen(line);
  48.     MyFSWriteNoCache(gRefNum, &len, line, nil);
  49. }
  50.  
  51.  
  52.  
  53. /*----------------------------------------------------------------------------
  54.     OpenLogFile 
  55.     
  56.     Open the log file.
  57. ----------------------------------------------------------------------------*/
  58.  
  59. void OpenLogFile (void)
  60. {
  61.     FCBPBRec pBlock;
  62.     FSSpec logFile;
  63.     OSErr err = noErr;
  64.     Str255 versStr, fileName;
  65.     CStr255 str, fmt;
  66.     Boolean empty;
  67.     
  68.     MyICReadSharedPrefs(kICeditorHelper);
  69.     
  70.     if (gRefNum != 0) return;
  71.     pBlock.ioNamePtr = nil;
  72.     pBlock.ioVRefNum = 0;
  73.     pBlock.ioRefNum = LMGetCurApRefNum();
  74.     pBlock.ioFCBIndx = 0;
  75.     err = PBGetFCBInfo(&pBlock, false);
  76.     if (err != noErr) goto exit;
  77.     GetPString(kStrLogFileName, fileName);
  78.     err = FSMakeFSSpec(pBlock.ioFCBVRefNum, pBlock.ioFCBParID, 
  79.         fileName, &logFile);
  80.     if (err != noErr && err != fnfErr) goto exit;
  81.  
  82.     err = OpenDataForkWriteCreateIfMissing(&logFile, gPrefs.savedArtCreator, 'TEXT',
  83.         smSystemScript, false, &gRefNum, &empty);
  84.     if (err != noErr) goto exit;
  85.  
  86.     PutString("");
  87.     PutString("------------------------------------------------------------------------------");
  88.     PutString("");
  89.     GetCString(kStrLogOpenMsg, str);
  90.     PutString(str);
  91.     err = GetVersionString(versStr);
  92.     if (err != noErr) goto exit;
  93.     GetCString(kStrNewsWatcherVersion, fmt);
  94.     p2cstr(versStr);
  95.     sprintf(str, fmt, versStr);
  96.     PutString(str);
  97.     GetCString(kStrLogLegend, str);
  98.     PutString(str);
  99.     PutString("");
  100.     return;
  101.     
  102. exit:
  103.  
  104.     if (gRefNum != 0) MyFSClose(gRefNum, nil);
  105.     gRefNum = 0;
  106.     ErrorMessageNumber(kStrLogCantOpen);
  107. }
  108.  
  109.  
  110.  
  111. /*----------------------------------------------------------------------------
  112.     CloseLogFile 
  113.     
  114.     Close the log file.
  115. ----------------------------------------------------------------------------*/
  116.  
  117. void CloseLogFile (void)
  118. {
  119.     CStr255 str;
  120.  
  121.     if (gRefNum == 0) return;
  122.     PutString("");
  123.     GetCString(kStrLogClosed, str);
  124.     PutString(str);
  125.     MyFSClose(gRefNum, nil);
  126.     gRefNum = 0;
  127. }
  128.  
  129.  
  130.  
  131. /*----------------------------------------------------------------------------
  132.     Log 
  133.     
  134.     Log a server command or response.
  135.     
  136.     Entry:    command = 
  137.                 'C' if command.
  138.                 'R' if response.
  139.                 ' ' if open/close.
  140.             serverAddr = IP address of server.
  141.             serverPort = server port number.
  142.             localPort = local port number.
  143.             str = command or response string or open/close message.
  144. ----------------------------------------------------------------------------*/
  145.  
  146. void Log (char logEntryType, unsigned long serverAddr, unsigned short serverPort, 
  147.     unsigned short localPort, char *str)
  148. {
  149.     char *filteredStr;
  150.     char line[512];
  151.  
  152.     if (gRefNum == 0) return;
  153.     if (logEntryType == 'C' && MyStrNEqual(str, "PASS", 4)) {
  154.         filteredStr = "PASS *******";
  155.     } else if (logEntryType == 'C' && MyStrNEqual(str, "AUTHINFO PASS", 13)) {
  156.         filteredStr = "AUTHINFO PASS *******";
  157.     } else {
  158.         filteredStr = str;
  159.     }
  160.     sprintf(line, "%c %lu.%lu.%lu.%lu %u %u %s",
  161.         logEntryType,
  162.         (serverAddr >> 24) & 0xff, (serverAddr >> 16) & 0xff, 
  163.         (serverAddr >> 8) & 0xff, serverAddr & 0xff,
  164.         serverPort, localPort, 
  165.         filteredStr);
  166.     PutString(line);
  167. }
  168.